package ch.usi.inf.pf2.editor;

import java.awt.Color;

import javax.swing.BorderFactory;

import ch.usi.inf.pf2.Sheet;
import ch.usi.inf.pf2.cell.Cell;
import ch.usi.inf.pf2.cell.Converter;

/**
 * Class that defines the cut action
 * With cut the new text is just an empty String
 * 
 * @author Lorenzo Baracchi, Ebrisa Savina Mastrodicasa
 *
 */
public class Cut extends TextManagement 
{

	/**
	 * Constructor for Cut
	 * @param sheet
	 * @param rowIndex the row index
	 * @param columnIndex the row column
	 */
	public Cut (Sheet sheet, int rowIndex, int columnIndex) 
	{
		super(sheet, rowIndex, columnIndex);
	}
	
	
	/**
	 * @return A sentence declaring the cut execute.
	 */
	public String toString ()
	{
		Converter conv = new Converter();
		conv.setCellReferenceNumbers(new int[] {this.getColumnIndex(), this.getRowIndex()});
		conv.convertToString();
		String cutString = "Cut the cell " + conv.getCellReference();
		return cutString;
	}
	
	
	/**
     * Save the cell in the old-fields for pasting them 
     * to some other cell or for possible undo actions. Then 
     * empty the cell where the cut has been executed.
     */
	@Override
	public void evaluate () 
	{
		System.out.println("Cut evaluate");
		Cell c = sheet.getCell(getColumnIndex(), getRowIndex());
		setOldText(c.getContent().toString());
		setNewText("");
		setOldColor(c.getColor());
		setNewColor(Color.WHITE);
		setOldBorder(c.getBorderType());
		setNewBorder(BorderFactory.createMatteBorder(2,5,2,5, Color.WHITE));
		sheet.insertDataAtCell(getNewText(), getColumnIndex(), getRowIndex());
		c.setColor(getNewColor());
		c.setBorderType(getNewBorder());
	}

	
	/**
     * Undo the previous cutting of a text.
     */
	@Override
	public void restore () 
	{
	    Cell c=sheet.getCell(getColumnIndex(), getRowIndex());
	    sheet.insertDataAtCell(getOldText(), getColumnIndex(), getRowIndex());
	    c.setColor(getOldColor());
	    c.setBorderType(getOldBorder());
	}

}
